<template>
  <div>
    <!-- 3、在页面中渲染（使用）组件 -->
    <home-header></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
  // 1、引入组件
  import homeHeader from './components/Header'
  import homeSwiper from './components/Swiper'
  import homeIcons from './components/Icons'
  import homeRecommend from './components/Recommend'
  import homeWeekend from './components/Weekend'
  import axios from 'axios'
  import { mapState } from 'vuex'

  export default {
    name: 'Home',
    components: {
      // 2、注册组件
      homeHeader,
      homeSwiper,
      homeIcons,
      homeRecommend,
      homeWeekend
    },
    data(){
      return {
        lastCity: '',
        swiperList: [],
        iconList: [],
        recommendList: [],
        weekendList: []
      }
    },
    methods: {
      getHomeInfo(){
        axios.get('/api/index.json?city=' + this.city)
          .then(this.getHomeInfoSucc)
      },
      getHomeInfoSucc(res){
        const resData = res.data.data
        if (res.data.ret && resData) {
          this.swiperList = resData.swiperList
          this.iconList = resData.iconList
          this.recommendList = resData.recommendList
          this.weekendList = resData.weekendList
        }
      }
    },
    mounted(){
      this.lastCity = this.city
      this.getHomeInfo()
    },
    activated (){
      if (this.lastCity !== this.city) {
        this.lastCity = this.city
        this.getHomeInfo()
      }
    },
    computed: {
      ...mapState(['city'])
    }
  }
</script>

<style  lang="stylus" scoped></style>
